# 开发环境启动脚本
param(
    [switch]$Auto,     # 自动启动所有服务
    [switch]$Frontend, # 仅启动前端
    [switch]$Backend,  # 仅启动后端
    [switch]$AI,       # 仅启动AI服务
    [switch]$Help      # 显示帮助
)

function Show-Help {
    Write-Host "=== 在线学习平台 - 开发环境启动脚本 ===" -ForegroundColor Green
    Write-Host ""
    Write-Host "用法:" -ForegroundColor Cyan
    Write-Host "  .\dev.ps1               显示启动指南"
    Write-Host "  .\dev.ps1 -Auto         自动启动所有服务"
    Write-Host "  .\dev.ps1 -Frontend     仅启动前端"
    Write-Host "  .\dev.ps1 -Backend      仅启动后端"
    Write-Host "  .\dev.ps1 -AI           仅启动AI服务"
    Write-Host "  .\dev.ps1 -Help         显示此帮助"
    Write-Host ""
}

function Start-Frontend {
    Write-Host "启动前端服务..." -ForegroundColor Yellow
    Set-Location frontend
    if (!(Test-Path "node_modules")) {
        Write-Host "安装前端依赖..." -ForegroundColor Cyan
        pnpm install
    }
    pnpm dev
}

function Start-Backend {
    Write-Host "启动后端服务..." -ForegroundColor Yellow
    Set-Location backend
    ./mvnw spring-boot:run
}

function Start-AI {
    Write-Host "启动AI服务..." -ForegroundColor Yellow
    Set-Location ai
    if (!(Test-Path ".venv")) {
        Write-Host "创建Python虚拟环境..." -ForegroundColor Cyan
        python -m venv .venv
    }
    .\.venv\Scripts\Activate.ps1
    pip install -r requirements.txt
    python main.py
}

function Test-Prerequisites {
    $errorList = @()
    
    # 检查Node.js
    if (!(Get-Command "node" -ErrorAction SilentlyContinue)) {
        $errorList += "Node.js 未安装"
    }
    
    # 检查pnpm
    if (!(Get-Command "pnpm" -ErrorAction SilentlyContinue)) {
        $errorList += "pnpm 未安装，请运行: npm install -g pnpm"
    }
    
    # 检查Java
    if (!(Get-Command "java" -ErrorAction SilentlyContinue)) {
        $errorList += "Java 未安装"
    }
    
    # 检查Python
    if (!(Get-Command "python" -ErrorAction SilentlyContinue)) {
        $errorList += "Python 未安装"
    }
    
    # 检查MySQL
    $mysqlService = Get-Service -Name "MySQL*" -ErrorAction SilentlyContinue
    if (!$mysqlService -or $mysqlService.Status -ne "Running") {
        $errorList += "MySQL 服务未运行"
    }
    
    if ($errorList.Count -gt 0) {
        Write-Host "检查到以下问题:" -ForegroundColor Red
        foreach ($errorItem in $errorList) {
            Write-Host "  - $errorItem" -ForegroundColor Red
        }
        return $false
    }
    
    Write-Host "✓ 所有依赖检查通过" -ForegroundColor Green
    return $true
}

# 主逻辑
if ($Help) {
    Show-Help
    exit
}

if ($Auto) {
    Write-Host "=== 自动启动模式 ===" -ForegroundColor Green
    
    if (!(Test-Prerequisites)) {
        exit 1
    }
    
    # 在新窗口中启动各服务
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; .\dev.ps1 -Frontend"
    Start-Sleep 2
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; .\dev.ps1 -Backend"
    Start-Sleep 2
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; .\dev.ps1 -AI"
    
    Write-Host "所有服务已在新窗口中启动！" -ForegroundColor Green
    Write-Host "访问地址:" -ForegroundColor Cyan
    Write-Host "  前端: http://localhost:5173" -ForegroundColor White
    Write-Host "  后端: http://localhost:8080/api/v1/health" -ForegroundColor White
    Write-Host "  AI服务: http://localhost:8001/health" -ForegroundColor White
    exit
}

if ($Frontend) {
    Start-Frontend
    exit
}

if ($Backend) {
    Start-Backend
    exit
}

if ($AI) {
    Start-AI
    exit
}

# 默认显示启动指南
Write-Host "=== 在线学习平台 - 开发启动指南 ===" -ForegroundColor Green
Write-Host ""
Write-Host "选择启动方式:" -ForegroundColor Cyan
Write-Host "  1. 自动启动 (推荐): .\dev.ps1 -Auto" -ForegroundColor Yellow
Write-Host "  2. 手动启动 (分别在3个终端运行):" -ForegroundColor Yellow
Write-Host ""

Write-Host "【终端1 - 前端】" -ForegroundColor Yellow
Write-Host "  .\dev.ps1 -Frontend" -ForegroundColor White
Write-Host "  访问: http://localhost:5173" -ForegroundColor Green
Write-Host ""

Write-Host "【终端2 - 后端】" -ForegroundColor Yellow
Write-Host "  .\dev.ps1 -Backend" -ForegroundColor White
Write-Host "  访问: http://localhost:8080/api/v1/health" -ForegroundColor Green
Write-Host ""

Write-Host "【终端3 - AI服务】" -ForegroundColor Yellow
Write-Host "  .\dev.ps1 -AI" -ForegroundColor White
Write-Host "  访问: http://localhost:8001/health" -ForegroundColor Green
Write-Host ""

Write-Host "=== 系统健康检查 ===" -ForegroundColor Green
Write-Host "前端健康检查页面: http://localhost:5173/health" -ForegroundColor Cyan
